<?php
namespace Admin\Controller;
use Util\AccessController;
use Admin\Util\lib\YZOauthClient;
use Admin\Util\lib\YZTokenClient;

/*
 * 微信登录拦截模块
 * 执行后S保存微信用户信息
 *
 */

class LoginController extends AccessController {

    public function eixt() {
        $this->S('_REMOVE_');
        redirect('Index/index');
    }

    public function index() {
        $this->display();
    }

    public function login(){
        $where['USER_NAME'] = $_POST['l_account'];
        $where['PWD'] = md5($_POST['l_password']);
        $LS = Service('Login','admin');
        $data = $LS->find($where);
        if($data){
            unset($data['PWD']);
            $this->S($data);

            $save['LOGINTIME'] = date('Y-m-d H:i:s');
            $save['LOGINIP'] = get_client_ip();
            $LS->update($where,$save);
            $this->redirect('Index/index');
        }else{
            $this->error('账号或密码错误');
        }
    }

    public function YZ_index() {
        $YZ_URL = C('YZ_URL');
        $URL = C('URL_ROOT');
        $CLIENT_ID = C('CLIENT_ID');
        header("Content-type:application/x-www-form-urlencoded");
        ini_set('date.timezone', 'Asia/Shanghai');
        Header("Location:{$YZ_URL}?client_id={$CLIENT_ID}&response_type=code&state=teststate&redirect_uri={$URL}");

    }

    public function Handle() {
        $CLIENT_ID = C('CLIENT_ID');
        $CLIENT_SECRET = C('CLIENT_SECRET');
        $redirectUrl = C('URL_ROOT');
        if ($_SESSION['access_token'] == '') {
            $token = new YZOauthClient($CLIENT_ID, $CLIENT_SECRET);
            $keys = array();
            $type = 'code';//如要刷新access_token，这里的值为refresh_token
            $keys['code'] = $_GET['code'];//如要刷新access_token，这里为$keys['refresh_token']
            $keys['redirect_uri'] = $redirectUrl;
            $access = $token->getToken($type, $keys);
            $access_token = $access['access_token'];
        } else {
            $access_token = $_SESSION['access_token'];
        }
        $this->S(array('access_token' => $access_token));
        $this->redirect('Index/index');
    }

    public function UpdatePWDIndex(){
        $this->display();
    }
    public function UpdatePWD(){
        $where['ID'] = $this->S('ID');
        $where['PWD'] = md5($_POST['OLDPWD']);
        $LS = Service('Login','admin');
        $data = $LS->find($where);
        $_POST['NEWPWD'] == $_POST['NEWPWD1'] ?1:$this->error('两次输入的新密码不一样');
        if($data){
            $save['PWD'] = md5($_POST['NEWPWD']);
            $save['LOGINTIME'] = date('Y-m-d H:i:s');
            $save['LOGINIP'] = get_client_ip();
            $UPOK = $LS->update($where,$save);
            $UPOK ? $this->success('修改成功'):$this->error('修改失败');
        }else{
            $this->error('原密码错误');
        }
    }


    /*
     * 微信登录
     */
    public function WxLoginIndex(){

    }
}

?>
